package com.itheima.config;




import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.proxy.DruidDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

public class JdbcConfig {


    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.initialSize}")
    private int initialSize;
    @Value("${jdbc.maxActive}")
    private int maxActive;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.maxWait}")
    private int maxWait;


    @Bean
    public DataSource dataSource(){

        DruidDataSource dr = new DruidDataSource();
        dr.setDriverClassName(driverClassName);
        dr.setUrl(url);
        dr.setInitialSize(initialSize);
        dr.setMaxActive(maxActive);
        dr.setMaxWait(maxWait);
        dr.setUsername(username);
        dr.setPassword(password);


        return dr;
    }

    public PlatformTransactionManager transactionManager(DataSource dataSource){

        DataSourceTransactionManager dtm = new DataSourceTransactionManager();

        dtm.setDataSource(dataSource);

        return dtm;


    }





}
